<?php

class ReferencesController extends Zend_Controller_Action {

    public function preDispatch() {
        $t = Zend_Registry::get('Zend_Locale');
        $this->view->navigation_line = array($t->_('References') => ADMIN_URL . '/references');
        $session = new Zend_Session_Namespace('UnCMS');
        if (!isset($session->acl->references))
            $this->_redirect(SITE_URL);
    }

    public function indexAction() {
        $model = new ReferencesModel();
        $this->view->list = $model->getRefs();
    }

    public function editAction() {
        $forms = new ReferencesForm();
        $rid = (int) $this->getRequest()->getParam('id', 0);
        $t = Zend_Registry::get('Zend_Locale');
        $form = $forms->Reference($rid);
        $this->view->form = $form;
        $model = new ReferencesModel();
        if ($rid > 0) {
            $formdata = $model->getRefs($rid);
            $this->view->formdata = $formdata;
            $this->view->navigation_line = $this->view->navigation_line + array($formdata['title'] => ADMIN_URL . '/references/view/id/' . $rid, $t->_('Edit reference') => ADMIN_URL . '/references/edit/id/' . $rid);
        } else {
            $this->view->navigation_line = $this->view->navigation_line + array($t->_('New reference') => ADMIN_URL . '/references/edit/id/0');
        }

        if ($this->_request->isPost()) {
            $Data = $this->_request->getPost();
            if ($form->isValid($Data)) {
                array_pop($Data);
                $rid = $model->updateReference($Data, $rid);
                $this->_redirect('/references/view/id/' . $rid);
            } else {
                $form->populate($Data);
            }
        }
        if ($rid > 0) {
            $form->populate($formdata);
        }
    }

    /*
     * Просмотр записей
     */

    public function viewAction() {
        $rid = (int) $this->getRequest()->getParam('id', 0);
        $t = Zend_Registry::get('Zend_Locale');
        $model = new ReferencesModel();
        $forms = new FormsModel();
        $ref = $model->getRefs($rid);
        $this->view->ref = $ref;
        $this->view->navigation_line = $this->view->navigation_line + array($ref['title'] => ADMIN_URL . '/references/view/id/' . $rid);
        $this->view->fields = $forms->getFields($ref['form']);
        $this->view->records = $model->getRecords($ref, $this->view->fields);
    }

    public function recordAction() {
        $rid = (int) $this->getRequest()->getParam('rid', 0);
        $id = (int) $this->getRequest()->getParam('id', 0);
        $t = Zend_Registry::get('Zend_Locale');
        $model = new ReferencesModel();
        $ref = $model->getRefs($rid);
        $forms = new FormsForm();
        $this->view->ref = $ref;
        if ($id > 0) {
            $record_data = $model->getRecord($ref, $id);
            $this->view->navigation_line = $this->view->navigation_line + array($ref['title'] => ADMIN_URL . '/references/view/id/' . $rid, $t->_('Edit record') => ADMIN_URL . '/references/record/rid/' . $rid . '/id/' . $id);
            $this->view->record = $record_data;
        } else {
            $this->view->navigation_line = $this->view->navigation_line + array($ref['title'] => ADMIN_URL . '/references/view/id/' . $rid, $t->_('New record') => ADMIN_URL . '/references/record/rid/' . $rid . '/id/0');
        }
        if ($ref['connection']){
            $form = $forms->getForm($ref['form'], ADMIN_URL . '/references/record/rid/' . $rid . '/id/' . $id, true);
        } else {
            $form = $forms->getForm($ref['form'], ADMIN_URL . '/references/record/rid/' . $rid . '/id/' . $id, false);
        }
        $this->view->form = $form;
        $formsmodel = new FormsModel();
        $fields = $formsmodel->getFields($ref['form']);
    
        if ($this->_request->isPost()) {
            $Data = $this->_request->getPost();
            if ($form->isValid($Data)) {
                array_pop($Data); //Удалили кнопку

                /*
                 * Проходимся по полям для их преобразования в БД
                 */
                foreach ($fields as $key => $value) {
                    if ($value->type == 'date')
                        $Data[$value->field] = date('Y-m-d', strtotime($Data[$value->field]));
                    if ($value->type == 'image') {
                        $imagesModel = new ImagesModel();
                        $imagesModel->updateImage($Data[$value->field], $value->image_width, $value->image_height, $value->preview, $value->preview_width, $value->preview_height);
                    }
                    if ($value->type == 'tags') {
                        $tagsModel = new TagsModel();
                        $tagsModel->updateTags($Data[$value->field], $value->identifier, $id);
                        $Data[$value->field] = '';
                    }
                }
                $id = $model->updateRecord($ref['table'], $Data, $id);
                $this->_redirect('/references/view/id/' . $rid);
            } else {
                $form->populate($Data);
            }
        } else {
            if ($id > 0) {
                foreach ($fields as $key => $value) {
                    if ($value->type == 'tags') {
                        $tagsModel = new TagsModel();
                        $record_data[$value->field] = $tagsModel->getObjectTags($value->identifier, $id, true);
                    }
                }
                $form->populate($record_data);
            }
        }
    }

    public function deleteAction() {
        $id = (int) $this->getRequest()->getParam('id', 0);
        $model = new ReferencesModel();
        $model->deleteRef($id);
        $this->_forward('index');
    }

    /*
     * delete record
     */

    public function removeAction() {
        $rid = (int) $this->getRequest()->getParam('rid', 0);
        $id = (int) $this->getRequest()->getParam('id', 0);
        $model = new ReferencesModel();
        $ref = $model->getRefs($rid);
        $model->deleteRecord($ref['table'], $id);
        $this->_redirect('/references/view/id/' . $rid);
    }

}
